﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using Leafing.Core.Logging;
using Leafing.Data;

using Wz16.Baoming.BLL;

public partial class frmList : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.btnDel.Attributes.Add("onclick", "javascript:return confirm('你确认要删除选中的记录吗?')");
        }
        if (!this.Page.User.Identity.IsAuthenticated)
        {
            Response.Redirect("Login.aspx");
        }

        if (this.ddlStatus.SelectedIndex > 0)
        {
            this.BmInfoDS1.Condition = (
                (CK.K[this.ddlCol.SelectedValue].LeftLike(txtContent.Text)
                || CK.K[this.ddlCol.SelectedValue].MiddleLike(txtContent.Text)
                || CK.K[this.ddlCol.SelectedValue].RightLike(txtContent.Text))
                && CK.K["Status"] == this.ddlStatus.SelectedValue);

        }
        else
        {
            this.BmInfoDS1.Condition = CK.K[this.ddlCol.SelectedValue].LeftLike(txtContent.Text)
                || CK.K[this.ddlCol.SelectedValue].MiddleLike(txtContent.Text)
                || CK.K[this.ddlCol.SelectedValue].RightLike(txtContent.Text);
        }
        this.BmInfoDS1.DefaultOrderBy = this.ddlOrder.SelectedValue;
    }

    private void BindData()
    {
        this.GridView1.PageSize = int.Parse( this.txtPageSize.Text );
        this.GridView1.DataSourceID = "BmInfoDS1";
  //      this.GridView1.DataBind();
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //如果是绑定数据行 
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                //           ((LinkButton)e.Row.Cells[0].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除：\"" + e.Row.Cells[2].Text + "\"吗?')");
            }

            //鼠标经过时，行背景色变 
            e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#E6F5FA'");
            //鼠标移出时，行背景色变 
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
        }
    }
    protected void btnShow_Click(object sender, EventArgs e)
    {
 //       this.GridView1.PageSize = int.Parse(txtPageSize.Text);
        this.BindData();
    }
    /// <summary>
    /// 删除记录
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDel_Click(object sender, EventArgs e)
    {
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
            if (cbox.Checked == true)
            {
                long id = (long)this.GridView1.DataKeys[i].Value;
                BmInfo bmInfo = BmInfo.FindById(id);
                Logger.Default.Warn( 
                    string.Format("用户：{0},操作:删除,对象：{1}", this.User.Identity.Name, bmInfo)
                    );
                bmInfo.Delete();
            }
        }
        this.BindData();
    }

    /// <summary>
    /// 改变报名状态
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnChangeStatus_Click(object sender, EventArgs e)
    {
        for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
        {
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
            if (cbox.Checked == true)
            {
                long id = (long)this.GridView1.DataKeys[i].Value;
                BmInfo bmInfo = BmInfo.FindById(id);
                bmInfo.Status = this.ddlStatusForChange.SelectedValue;
                bmInfo.Save();
                Logger.Default.Info(
                    string.Format("用户：{0},操作:更改报名状态,对象：{1}", this.User.Identity.Name, bmInfo)
                    );
            }
        }
        this.BindData();
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        this.BindData();
        this.hlPrintHuizong.Target = "_blank";
        this.hlPrintHuizong.Text = String.Format("{0}{1}汇总表", DateTime.Now.Year,this.ddlStatus.SelectedValue);
        this.hlPrintHuizong.NavigateUrl = String.Format("frmPrintHuizong.aspx?Status={0}&OrderBy={1}", Server.UrlEncode( this.ddlStatus.SelectedValue ),this.ddlOrder.SelectedValue );

        this.hlPrintBaoming.Target = "_blank";
      //  this.hlPrintBaoming.Text = String.Format("{0}{1}汇总表", DateTime.Now.Year, this.ddlStatus.SelectedValue);
        this.hlPrintBaoming.NavigateUrl = String.Format("frmPrint.aspx?Status={0}&OrderBy={1}", Server.UrlEncode(this.ddlStatus.SelectedValue), this.ddlOrder.SelectedValue);
    }
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.BindData();
    }
    protected void BmInfoDS1_Init(object sender, EventArgs e)
    {

    }
}
